/*
 * Segue Audio Player
 * Copyright 2011 Fraser McCrossan frasermccrossan@gmail.com
 * 
 * This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published
 *  by the Free Software Foundation, either version 3 of the License,
 *  or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

package com.frasermccrossan.segue;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class BrowseLibrary extends ListActivity implements OnItemClickListener {

	public QueuePlayer mQueuePlayer = null;
	public boolean mIsBound;
	ListView mListView;
	
	Toast mToast;
	
	ContentResolver cr;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
    	cr = getContentResolver();
    	setupList();
    	mListView = getListView();
    	mListView.setOnItemClickListener(this);
    	mListView.setEnabled(false);
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mQueuePlayer = ((QueuePlayer.QueueBinder)service).getService();
            mListView.setEnabled(true);
        }

        public void onServiceDisconnected(ComponentName className) {
            mQueuePlayer = null;
        }
    };
    
    @Override
    public void onStart() {
    	super.onStart();
    	mIsBound = bindService(new Intent(this, QueuePlayer.class), mConnection, Context.BIND_AUTO_CREATE);
    }
    
    @Override
    protected void onStop() {
    	if (mIsBound) {
            unbindService(mConnection);
            mIsBound = false;
    	}
    	super.onStop();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

	CharSequence[] list_strings;

    // this does everything necessary to attach the main List to an adapter
    private void setupList() {
    	setListString();
    	setListAdapter(new ArrayAdapter<CharSequence>(BrowseLibrary.this,
    											      android.R.layout.simple_list_item_1,
    											      list_strings));
    }
    
    // returns the list of items that should be in the main list
    // override to do something different
    public void setListString() {
    	Resources res = getResources();
    	String listStrings[] =  {
    			res.getString(R.string.artists),
    			res.getString(R.string.albums),
    			res.getString(R.string.recent_played),
//    			res.getString(R.string.random),
//    			res.getString(R.string.similar),
    	};
    	list_strings = listStrings;
    }

    public void gotoPlayingScreen() {
    	Intent intent = new Intent(this, PlayingScreen.class);
    	intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    	startActivity(intent);
    }
	
    void gotoPreferences() {
    	Intent prefs = new Intent(this, MainPreferences.class);
    	startActivity(prefs);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.browse_screen_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.playing_screen:
            gotoPlayingScreen();
            return true;
        case R.id.browse_prefs:
            gotoPreferences();
            return true;
        case R.id.clear_queue:
        	mQueuePlayer.clearQueue();
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    // override to do something different
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    	// this has to follow the order of items[] above
    	switch (position) {
    	case 0:
    		startActivity(new Intent(BrowseLibrary.this, BrowseArtists.class));
    		break;
    	case 1:
    		startActivity(new Intent(BrowseLibrary.this, BrowseAlbums.class));
    		break;
    	case 2:
    		startActivity(new Intent(BrowseLibrary.this, BrowseRecentlyPlayed.class));
    		break;
    	case 3:
    		Track newTrack =  mQueuePlayer.getRandomTrack();
    		if (newTrack != null) {
    			mQueuePlayer.addTrack(newTrack);
    			proclaim(String.format(getString(R.string.adding_track), newTrack.title));
    		}
    		break;
    	}    	
    }
    
    public void proclaim(CharSequence message) {
    	if (mToast != null) {
    		mToast.setText(message);
    		mToast.show();
    	}
    }
    
    public void proclaim(int stringResId) {
    	proclaim(getResources().getString(stringResId));
    }
}